<!DOCTYPE html>
<?php 
/*
$link = mysql_connect('hostname','dbuser','dbpassword'); 
if (!$link) { 
	die('Could not connect to MySQL: ' . mysql_error()); 
} 
echo 'Connection OK'; 
mysql_close($link);
*/ 
?> 

<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
  html { height: 100% }
  body { height: 100%; margin: 0; padding: 0 }
  #map_canvas { height: 100% }
</style>
<script type="text/javascript"
    src="http://maps.googleapis.com/maps/api/js?sensor=true">
</script>
<script type="text/javascript">
  function initialize() {
    var latlng = new google.maps.LatLng(-19.26772, 146.78284);
    var myOptions = {
      zoom: 8,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById("map_canvas"),
        myOptions);
		
		
		$all_inputs = document.getElementsByTagName('input');
		
		
		var name_pat=/name=[^&]*/i;
		var lat_pat=/long=[^&]*/i;
		var long_pat=/lat=[^&]*/i;
		
		var i = 0;
		for (i ; i < $all_inputs.length; i++) {
		   if  ($all_inputs[i].className == 'station_data') {
				var value = $all_inputs[i].value;
				createMarker(map, new Number(new String(lat_pat.exec(value)).substring(5)), new Number(new String(long_pat.exec(value)).substring(4)), new String(name_pat.exec(value)).substring(5));	
			}
		}
		
		
		
  }
  
  function createMarker(map, lat, longitude, marker_title) {
	  var myLatlng = new google.maps.LatLng(longitude, lat);
		  
	  var marker = new google.maps.Marker({
		  position: myLatlng, 
		  map: map, 
		  title: marker_title
	  });   
  }

</script>
</head>
<body onLoad="initialize()">


<?php

$connection = mysql_connect('localhost', 'admin', 'admin');
mysql_query('USE aws;');
$result = mysql_query('SELECT * from `weatherstation`;');
if($result) {
	$names = array();
	
	$count = 0;
	
	echo "<form action='home.php' method='post'>";
		while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
			$names[] =  $row["aws_name"];
			$count++;
			if (!isset($_POST['weather_stations']) ||  $_POST['weather_stations'] == $row["aws_name"]) {
				echo "<input class='station_data' id='station_" . $count . "' type='hidden' value='id=".$row["aws_id"]."&name=".$row["aws_name"]."&lat=".$row["aws_latitude"]."&long=".$row["aws_longitude"]."' />";
			}
		}
	echo "<br />";		
	echo "<select id='weather_stations' name='weather_stations'>";
	
	echo "<option>Select...</option>";
}
for($i = 0; $i<count($names); $i++) {
				echo "<option" . ((isset($_POST['weather_stations']) && $_POST['weather_stations'] == $names[$i]) ? " selected='selected'" : "") . ">" ;
				echo "$names[$i]";
				echo "</option>";
				}
 		
			
 ?>


</select>	
<input type='submit' value='submit' />
</form>
<?php
mysql_close($connection);
?>


<table border=0 align="center">

  <!--
    Insert each map into a table cell using the div tag.
    Control the map and cell size using the width and height
    attributes of the div tag.
  -->
<tr><td height="76" colspan="2"></td></tr>
<tr>
  <td height="77" colspan="2">
	
  </td>
</tr>
  <tr>
    <td width="510">
              <div id="map_canvas" style="width: 400px; height: 300px;"></div>
    </td><td width="576">
   
</td></tr>

</table>
<?php
	
if (isset($_POST['weather_stations'])) {





	$connection = mysql_connect('localhost', 'admin', 'admin');
mysql_query('USE aws;');
$query = "SELECT * FROM weatherstation JOIN weatherdetails ON weatherstation.aws_id = weatherdetails.aws_id WHERE weatherstation.aws_name = '" . $_POST['weather_stations'] . "'";
$result = mysql_query($query);
Print "<table border=1>";
Print "<tr>";
Print "<th>AWS Name</th><th>Temperature: </th><th> Rainfall</th><th> Humidity</th><th> Pressure</th><th> Wind Direction</th><th>Wind Gust</th><th> Dew Point</th><th> AWS ID</th>";

while($info = mysql_fetch_array($result))
{
Print "<tr>";
Print "<td>".$info['aws_name']."</td>";
Print "<td>".$info['Temperature']."</td>";
Print "<td>".$info['Rainfall']."</td>";
Print "<td>".$info['Humidity']."</td>";
Print "<td>".$info['Pressure']."</td>";
Print "<td>".$info['WindDirection']."</td>";
Print "<td>".$info['WindGust']."</td>";
Print "<td>".$info['DewPoint']."</td>";
Print "<td>".$info['aws_id']."</td></tr>";
}
?>
</body>
</html>